package other.her.system.client;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.exception.CacheException;
import com.rkhd.platform.sdk.exception.XsyHttpException;
import com.rkhd.platform.sdk.http.CommonData;
import com.rkhd.platform.sdk.http.CommonHttpClient;
import com.rkhd.platform.sdk.http.HttpResult;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import other.her.system.utils.ResultVo;

import java.util.*;

/**
 * @Description erp数据库对接
 * @Author 黎炎松
 * 2024-09-30 10:39
 */
public class ErpUtils {
    private static String BASE_URL = "http://47.92.3.51:8089"; // 域名 47.92.3.51  121.89.218.72
    private static String GET_DATA =  "/getData"; // 查询数据
    private static String EXCEL_SQL =  "/exceSql"; // 执行sql语句
    private static final Logger LOGGER = LoggerFactory.getLogger();

    public static void main(String[] args) throws CacheException, XsyHttpException {
        //query("SELECT top 1 t0.FNumber,t0.FName,t4.FFullName,t0.FModel,t0.FHelpCode,t1.FVersion,t2.FQtyMax,t2.FQtyMin,t3.FProfitRate,t3.FClass " +
        //        "  from t_ICItemCore t0 \n" +
        //        "  LEFT JOIN t_ICItemDesign t1 on t0.FItemID = t1.FItemID\n" +
        //        "  LEFT JOIN t_ICItemPlan t2 on t0.FItemID = t2.FItemID\n" +
        //        "  LEFT JOIN t_ICItemMaterial t3 on t0.FItemID = t3.FItemID\n" +
        //        "  LEFT JOIN t_ICItemBase t4 on t0.FItemID = t4.FItemID", BASE_URL);

        //executeSql("delete from crm_tableDataChange where id in (167)", BASE_URL);
    }


    /**
     * 获取数据
     * @param sql
     * @return
     * @throws XsyHttpException
     */
    public static List<JSONObject> query(String sql, String domain) {
        LOGGER.info("sql ==> " + sql);
        List<JSONObject> resultList = new ArrayList<>();
        CommonData build = CommonData.newBuilder().callString(domain + GET_DATA)
                .callType("POST")
                .formData("sql", sql)
                .build();
        HttpResult execute = CommonHttpClient.instance().execute(build);
        JSONObject result = JSONObject.parseObject(execute.getResult());

        if (result.getInteger("code") != 200) {
            LOGGER.error("查询异常 ==> " + result.getString("msg"));
            return resultList;
        }

        return JSONArray.parseArray(result.getString("data"), JSONObject.class);
    }


    /**
     * 获取数据
     * @param sql
     * @return
     * @throws XsyHttpException
     */
    public static JSONObject queryOne(String sql, String domain) {
        LOGGER.info("sql ==> " + sql);
        List<JSONObject> resultList = new ArrayList<>();
        CommonData build = CommonData.newBuilder().callString(domain + GET_DATA)
                .callType("POST")
                .formData("sql", sql)
                .build();
        HttpResult execute = CommonHttpClient.instance().execute(build);
        JSONObject result = JSONObject.parseObject(execute.getResult());
        if (result.getInteger("code") == 200) {
            resultList = JSONArray.parseArray(result.getString("data"), JSONObject.class);
        }
        if (resultList.size() <= 0) {
            return null;
        }
        return resultList.get(0);
    }

    public static ResultVo executeSql(String sql, String domain) {
        LOGGER.info("sql ==> " + sql);
        CommonData build = CommonData.newBuilder().callString(domain + EXCEL_SQL)
                .callType("POST")
                .formData("sql", sql)
                .build();
        HttpResult execute = CommonHttpClient.instance().execute(build);
        JSONObject result = JSONObject.parseObject(execute.getResult());
        LOGGER.info("result ==> " + result);
        if (result.getInteger("code") != 200) {
            return ResultVo.error(result.getString("msg"));
        }
        return ResultVo.success();
    }



}
